package src.main.java.org.netWorkProgram.UDP.demo2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/*
* 实现UDP通信-多发多收
* */
public class Client {
    public static void main(String[] args) throws Exception {
        //1.创建客户端对象
        DatagramSocket socket = new DatagramSocket();
        //2.创建数据包对象封装要发出去的数据
        //  public DatagramPacket(byte buf[], int length,InetAddress address, int port)
        /*
        * buf[] 封装要发出去的数据
        * length:发送出去的字节大小
        * address:服务端IP地址
        * port:服务端端口
        * */
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请说：");
            String msg = scanner.nextLine();
            //退出
            if("exit".equals(msg)){
                System.out.println("欢迎下次光临！");
                socket.close();
                break;
            }

            byte[] bytes = msg.getBytes();
            DatagramPacket packet = new DatagramPacket(bytes,bytes.length,
                    InetAddress.getLocalHost(),6666);
            //3.开始发送
            socket.send(packet);
        }
    }
}
